var Audio = Audio || function() {
      var self  = this;
      self.play = self.stop = new Function();
    };


var snowStorm = snowStorm || {};

snowStorm.className = "snow";

var Lottery = (function($container) {

  var timer           = null
      , itemWidth       = 142
      , itemCount       = 0
      , curPos          = 0
      ;

  var stopAudio       = new Audio("res/ping.mp3")
      , backAudio       = new Audio("res/back.mp3")
      ;


  backAudio.loop = true;

  var  $content        = $container.children("ul")
      , $hero           = $("#lottery-hero span")
      ;
  var index = 0;

  var candicates,selected;

  var init = function() {

    $.ajax({
      url: $container.data("url")
      , dataType: "text"
      , success: function(data) {
        var names = data.toString().split('\r\n');
        candicates = names;
        show();
      }
    });

  };

  var show = function(){

    var showCandicates = [];
    var localIndex = index;
    var max = Math.min(5, candicates.length);
    for(var i = 0; i < max; i ++){
      showCandicates.push(candicates[localIndex]);
      if(localIndex == candicates.length - 1){
        localIndex = 0;
      } else {
        localIndex++;
      }
    }

    var html = "";
    for (var i = 0; i < showCandicates.length; i++) {
      var name = showCandicates[i];
      html += ("<li><b>" + name + "</b></li>");
    }
    $content.html(html);
  }

  var start  = function() {
    if(Lottery.running()){
      return;
    }
    if(!isNaN(selected)){
      //candicates.splice(selected,1);
      selected = null;
    }

    //if(candicates.length == 0){
    //  alert('木有啦.');
    //  return;
    //}

    clearInterval(timer);

    backAudio.play();
    stopAudio.pause();
    $(document.body).addClass("stop");

    timer = setInterval(function() {

      //curPos = parseInt($content.css("left")) | 0;
      //curPos -= itemWidth / 2;
      //
      //(curPos < 0 - itemWidth * itemCount) && (curPos = 0);
      //
      //$content.css("left", curPos);
      if(index >= candicates.length - 1){
        index = 0;
      } else {
        index++;
      }

      show();

    }, 10);

    $hero.hide();
  };

  var stop = function() {
    clearInterval(timer);
    timer = null;

    backAudio.pause();
    stopAudio.play();
    $(document.body).removeClass("stop");

    selected  = getCurIdx();

    setCurIdx(selected);
  };

  var running = function() {
    return timer != null;
  };

  //Index: first item on the left
  var setCurIdx = function(idx) {

    var selected = candicates[idx];
    $hero.html(selected).show('slow');

  };

  var getCurIdx = function() {
    return index;
  };


  return {
    init: init
    , start: start
    , stop: stop
    , running: running
    , setCurIdx: setCurIdx
    , getCurIdx: getCurIdx
  };

})($("#lottery-container"));



$(document).ready(function() {
  Lottery.init();
});

$(document).keydown(function(e) {
  var key = e.keyCode;
  if (key != 32 && key != 13) return;

  Lottery.running() ? Lottery.stop() : Lottery.start();
});